Custom Storage Client
Overview
The StorageClient interface is designed to be implemented by platform-specific storage solutions, such as SQLite, ObjectBox, Custom Hardware or FileSystem. This interface allows the Mobile SDK to interact with the storage layer without being tied to a specific implementation.
This guide provides a demo implementation of in-memory client. This is useful for testing or development purposes, but it is not suitable for production use. But we can create a custom storage provider to persist data.
Implementing a Custom StorageClient
App.tsx
package com.silencelaboratories.silentshard
import com.silencelaboratories.storage.StorageClient
import com.silencelaboratories.storage.silentshard.ReconcileStoreDao
class CustomStorageClient : StorageClient<ReconcileStoreDao> {
/**
* Representing in-memory database. In real world it should be some SQL based DB or
* secure storage or custom hardware. It's up to the implementation app's use-case.
*
* */
private val keyshareDaoSet = mutableSetOf<ReconcileStoreDao>()
/**
* Write all the fields of ReconcileStoreDao to the storage. The dao contains the
* current state of the keyshare from SDKs POV.
* */
override suspend fun write(
dao: ReconcileStoreDao,
) {
// Write to storage.
if (!keyshareDaoSet.add(dao)) {
keyshareDaoSet.remove(dao)
keyshareDaoSet.add(dao)
}
}
/**
* Read all the fields of ReconcileStoreDao from the storage. The dao contains the
* current state of the keyshare.
* */
override suspend fun read(
key: String,
): ReconcileStoreDao? {
// Read from storage.
return keyshareDaoSet.find { it.keyId == key }
}
}